package com.mixsmart.commonweb.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mixsmart.commonweb.IWebConstant;
import com.mixsmart.commonweb.bean.TreeProp;
import com.mixsmart.commonweb.bean.entity.TCMenu;
import com.mixsmart.commonweb.bean.entity.TCRole;
import com.mixsmart.commonweb.service.IRoleService;
import com.mixsmart.commonweb.service.IRoleUrlResService;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.spellsql.utils.Cnd;
import com.mixsmart.spellsql.utils.Exp;
import com.mixsmart.utils.ArrayUtils;
import com.mixsmart.utils.CollectionUtils;
import com.mixsmart.utils.StringUtils;

/**
 * 角色服务实现类
 * @version 1.0
 * @since JDK1.6以上
 * @author lmq
 * <br />
 * 2016年2月1日
 */
@Service
public class RoleService extends WebMgrService<TCRole> implements IRoleService {

	@Autowired
	private RoleMenuService roleMenuServ;
	@Autowired
	private IRoleUrlResService roleUrlResServ;
	
	@Override
	public String getSuperRoleId() {
		String roleId = null;
		TCRole role = find(Cnd.where(Exp.eq("flag", IWebConstant.ROLE_SUPER_ADMIN)));
		if(null != role) {
			roleId = role.getId();
		}
		return roleId;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public boolean delete(String roleId) {
		boolean is = false;
		if(StringUtils.isNotEmpty(roleId)) {
			String[] ids = roleId.split(IWebConstant.MULTI_VALUE_SPLIT);
			List<TCRole> roles = super.finds(Cnd.where(Exp.in("id", ids)));
			if(!isContainAdminRole(roles)) {
				HashMap<String, Object> param = new HashMap<String, Object>(1);
				param.put("ids", ids);
				MixResponse<String> mixResp = getExecuteServ().executes("del_cascade_role", param);
				is = IWebConstant.OP_SUCCESS.equals(mixResp.getResult());
				param = null;
			} else {
				String msg = "删除的角色中含有超级管理员角色，不能删除";
				logger.error(msg);
				throw new IllegalArgumentException(msg);
			}
		}
		return is;
	}
	
	/**
	 * 判断角色列表中是否含有超级管理员角色
	 * @param roles 角色列表
	 * @return 如果含有超级管理员角色则返回：true；否则返回false
	 */
	private boolean isContainAdminRole(List<TCRole> roles) {
		boolean is = false;
		if(CollectionUtils.isNotEmpty(roles)) {
			for (TCRole role: roles) {
				if(role.getFlag().equals(IWebConstant.ROLE_SUPER_ADMIN)) {
					is = true;
					break;
				}
			}
		}
		return is;
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public boolean save(TCRole role,String[] menuIds,List<TreeProp> treeProps) {
		boolean isSuccess = false;
		if(null != role && super.save(role)) {
			if(StringUtils.isNotEmpty(role.getId())) {
				if(null != menuIds && menuIds.length>0 ) {
					List<TCMenu> menus = super.finds(TCMenu.class, Cnd.where(Exp.in("id", menuIds)));
					if(null != menus && menus.size()>0) {
						roleMenuServ.save(role.getId(), menus);
					}
					menus = null;
				}
				if(CollectionUtils.isNotEmpty(treeProps))
					 roleUrlResServ.save(role.getId(), treeProps);
				treeProps = null;
				menuIds = null;
				isSuccess = true;
			} 
			menuIds = null;
		}
		return isSuccess;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public boolean update(TCRole role, String[] menuIds, List<TreeProp> treeProps) {
		boolean is = false;
		if(null != role && super.update(role)) {
			//删除角色与菜单的关联关系
			roleMenuServ.delete(Cnd.where(Exp.eq("roleId", role.getId())));
			if(ArrayUtils.isNotEmpty(menuIds)) {
				//删除后再重新保存角色与菜单的关联关系
				List<TCMenu> menus = super.finds(TCMenu.class, Cnd.where(Exp.in("id", menuIds)));
				if(null != menus && menus.size()>0) {
					roleMenuServ.save(role.getId(), menus);
				}
				menus = null;
			}
			roleUrlResServ.update(role.getId(), treeProps);
			is = true;
		}
		return is;
	}
}
